//
//  MMLoginController.m
//  MiaMind
//
//  Created by Vill on 2023/11/23.
//

#import "MMLoginController.h"
#import "MMLoadingBtn.h"
#import "AIRegisterController.h"
@interface MMLoginController ()

@property (nonatomic, strong) UIImageView *bgImg;
@property (nonatomic, strong) UIImageView *titleImg;
@property (nonatomic, strong) MMLoadingBtn *loginBtn;

@end

@implementation MMLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.bgImg];
    [self.view addSubview:self.titleImg];
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    [self.titleImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).mas_offset(KNavigationBarHeight + PTW(20));
        make.width.mas_equalTo(PTW(175));
    }];
    [self.view addSubview:self.loginBtn];
    
    AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
    NSLog(@"model --- %@",model.token);
}

- (UIImageView *)bgImg{
    if (!_bgImg){
        _bgImg = [UIImageView new];
        _bgImg.image = KImaged(@"ai_main_bg");
        _bgImg.contentMode = UIViewContentModeScaleAspectFill;
        _bgImg.clipsToBounds = YES;
    }
    return _bgImg;
}

- (UIImageView *)titleImg{
    if (!_titleImg){
        _titleImg = [UIImageView new];
        _titleImg.image = KImaged(@"ai_main_appName");
    }
    return _titleImg;
}

- (MMLoadingBtn *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [MMLoadingBtn loadingWithFrame:CGRectMake(PTW(33), KScreenHeight - PTW(240), KScreenWidth - PTW(66), PTW(56)) andBackgroundColor:[UIColor colorHex:@"#FFFFFF" alpha:0.3] andTitle:@"Start" andTitleColor:[UIColor colorHex:@"#FFFFFF" alpha:1] andTitleFont:[UIFont fontWithWeight:MTBold size:PTW(17)] andCornerRadius:PTW(28) andClickBlock:^{
            [self loginBtnAction];
        }];
    }
    return _loginBtn;
}

- (void)loginBtnAction{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"deviceNo"] = KDeviceUUID;
    [AIManager requestMethod:@"POST" url:api_Login parameters:param successBlock:^(id  _Nullable result) {
        NSLog(@"result ------ %@",result);
        AIUserInfoModel *model = [AIUserInfoModel mj_objectWithKeyValues:result];
        [[AIUserInfoManager shareManager] updateUserInfo:model];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.loginBtn stopAnimateAndCallBack:^{
                AIRegisterController *reg = [AIRegisterController new];
                [self.navigationController pushViewController:reg animated:YES];
            }];
        });
    } failedBlock:^(NSError * _Nullable error) {
        NSLog(@"%@",error);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.loginBtn stopAnimateAndCallBack:^{
            }];
        });
    } showHUD:NO];
}

@end
